* ============================================================================ *
* PROJECT:		The making of radical right voters: persuasion and contrast effects in a dynamic political context
* AUTHOR: 		Ka Ming Chan
* DATE:			2021-12-30
* ============================================================================ *

* ---------------------------------------------------------------------------- *
* Settings and Open Data
* ---------------------------------------------------------------------------- *
clear
set more off
// ssc install blindschemes
// ssc install combomarginsplot
// ssc install carryforward
// ssc install reghdfe  
// ssc install ftools
// net sj 20-2
// net install st0596 
// net get st0596 
//net install grc1leg, from(http://www.stata.com/users/vwiggins) replace
*** you may need the above command if these schemes are not installed in your computer  
set scheme plotplain   
cd ""
use "data.dta", replace
des

* ----------------------------------------------------------------------------
* Persuasion effect								 
* ---------------------------------------------------------------------------- 
* declare panel 
tsset id wave  
drop if wave==0 | wave ==6 // wave 0 is the pilot phase; wave 6 is the wave collected after the 2017 federal election 

* generate regression sample
gen persuasion_sample_1 = 1 if pos_!=. & AfD!=. & region!=.

* ------------------------------------------------------------------------------
* regression model testing if there is a change in left/right self-placement among 1st-time AfD voters and non-AfD voters
eststo ife_pos: xtreg pos_ c.LT i.mig_crisis c.LT#AfD c.LT#i.mig_crisis 1.mig_crisis#AfD i.mig_crisis#AfD#c.LT if persuasion_sample_1==1 [pweight=dw_std], fe 
quietly estadd local fixedi "Yes", replace
* the difference in self-placement between AfD voters and non-AfD voters at wave 1
predict fe_pos, u
tabulate AfD, summarize(fe_pos) 

* ------------------------------------------------------------------------------
* Figure 5 panel a. Margin plot of left/right self-placement of 1st-time AfD voters and non-AfD voters across the 5 waves
margins if AfD==0, exp(predict(xb) - 0.06216766) at(mig_crisis=(0) LT=(0 0.3125)) ///
	at(mig_crisis=(1) LT=(0.5 0.75 0.8125)) noestimcheck saving(afd0 ,replace)
margins if AfD==1, exp(predict(xb) + 0.56865026) at(mig_crisis=(0) LT=(0 0.3125)) ///
	at(mig_crisis=(1) LT=(0.5 0.75 0.8125)) noestimcheck saving(afd1 ,replace)
combomarginsplot afd0 afd1, labels("non-AfD voters" "1st time AfD voters") /// you may need to install combomarginsplot
	xtitle("No. of subnational parliament entered") xlabel(0"0" 0.3125"5" 0.5"8" 0.75"12" 0.8125"13") ///
	ytitle("Left/right self-placement") title("(a) Change in left/right self-placement")  legend(position(6) col(2))  ylabel(1(1)7) ///
	plot2opts(lcolor("0 158 224"%70) mcolor("0 158 224"%70)) ci2opts(lcolor("0 158 224"%70)) saving(persuasion_expanded_sim, replace) 


* ------------------------------------------------------------------------------
* t-test for right-wing shift in self-placement 
gen right1=.
replace right1=1 if persuasion_sample_1==1 & AfD==1 & wave==1 & pos_>4 // 1st time AfD voters' whose ideological position is >4 on a 1-7 scale in wave 1
replace right1=0 if persuasion_sample_1==1 & AfD==1 & wave==1 & pos_<=4 // 1st time AfD voters' whose ideological position is <=4 on a 1-7 scale in wave 1
tsfill
bysort id: carryforward right1, gen(right_1) 
drop right1
gen right_5=.
replace right_5=1 if persuasion_sample_1==1 & AfD==1 & wave==5 & pos_>4 // 1st time AfD voters' whose ideological position is >4 on a 1-7 scale in wave 5
replace right_5=0 if persuasion_sample_1==1 & AfD==1 & wave==5 & pos_<=4 // 1st time AfD voters' whose ideological position is >4 on a 1-7 scale in wave 5

gen rwsample=1 if right_1!=. & right_5!=.
gen rw_sample = rwsample
gsort id -wave
by id : replace rw_sample = rw_sample[_n-1] if missing(rwsample)
drop rwsample
sum pos_ if rw_sample==1 & wave==1 [aweight=dw_std]
sum pos_ if rw_sample==1 & wave==5 [aweight=dw_std] 
reg right_5  right_1 [pweight=dw_std]

* ------------------------------------------------------------------------------
* Figure 5 panel b. Histogram for right-wing shift in self-placement among 1st-time AfD voters
twoway  (hist pos_ if rw_sample==1 & wave==1, lcolor("0 0 0 %50") fcolor("0 0 0 %50") discrete) || /// 
	(hist pos_ if rw_sample==1 & wave==5, lcolor("0 158 224 %60") fcolor("0 158 224 %60") discrete), ///
	xlabel(1(1)7) xtitle("Left/right self-placement") ///
	xline(4.410953, lcolor(black) lw(medthick)) xline(4.800276, lcolor("0 158 224") lpattern(solid) lw(medthick)) ///
	ytitle("Density") title("(b) left/right self-placement among first-time AfD voters") ///
	legend(order(1 "wave 1" 2 "wave 5" ) position(6) col(2)) saving(persuasion_hist, replace) 
graph combine persuasion_expanded_sim.gph persuasion_hist.gph
graph export "persuasion.png", as(png) replace


* ----------------------------------------------------------------------------
* Cultural Attitude
* ---------------------------------------------------------------------------- 
* generate regression sample
gen persuasion_sample_2 = 1 if cul_useful_!=. & cul_sol_!=. & cul_att_!=. & cul_like_!=. & ///
	cul_useful_!=.a & cul_sol_!=.a & cul_att_!=.a & cul_like_!=.a & AfD!=. & region!=.


* regression model testing if there is change in cultural attitude for all 4 items 
foreach var in cul_useful_ cul_sol_ cul_att_ cul_like_ {
	eststo ife_`var': reghdfe `var' c.LT  c.LT#AfD  if persuasion_sample_2==1 [pweight=dw_std], absorb(id) // IFE
	quietly estadd local fixedi "Yes", replace
}

* ------------------------------------------------------------------------------
* Table S3.1 Persuasion effect on left/right self-placement and cultural attitude
esttab  ife_pos ife_cul_useful_  ife_cul_sol_ ife_cul_att_ ife_cul_like_  ///
	using "persuasion_lr+cul.rtf", b(2) se(2) replace ///
	scalars ("fixedi Individual FE" "N Observations" "r2 R-squared") ///
	label wide note onecell nobaselevels  noomitted nonumbers  lines varwidth(30) ///
	order (LT  1.AfD#c.LT 1.mig_crisis 1.mig_crisis#1.AfD) ///
	coeflabels(1.AfD#c.LT "1st time AfD voters * subnational parliament"  ///
	1.mig_crisis "post-crisis" 1.mig_crisis#1.AfD "1st time AfD voters * post-crisis" ///
	1.mig_crisis#c.LT "post-crisis * subnational parliament" 1.mig_crisis#1.AfD#c.LT "1st time AfD voters * post-crisis * subnational parliament") ///
	mtitles("Model 1" "Model 2" "Model 3" "Model 4"	"Model 5") modelwidth(4) ///
	note ("Note: DV of Model 1: Left/right self-placement;" ///
	"DV of Model 2: Usefulness of cultural diversity;" ///
	"DV of Model 3: High cultural diversity can tackle new problems;" ///
	"DV of Model 4: Positive attitudes towards other cultures;" ///
	"DV of Model 5: Liking people from different cultural backgrounds." ///
	"Entries are coefficients of fixed effect model. Standard errors are shown in parentheses.") ///
	title ("Table S3.1 Persuasion effect on left/right self-placement and cultural attitude") 


* ------------------------------------------------------------------------------
* Table S3.2 Persuasion effect on left/right self-placement and cultural attitude (Ordinal logit model)
eststo ologit_pos: feologit pos_ c.LT i.mig_crisis c.LT#AfD c.LT#i.mig_crisis 1.mig_crisis#AfD i.mig_crisis#AfD#c.LT if persuasion_sample_1==1 [pweight=dw_std], group(id)  
quietly estadd local fixedi "Yes", replace
foreach var in cul_useful_ cul_sol_ cul_att_ cul_like_ {
	eststo ologit`var': feologit `var' c.LT c.LT#AfD if persuasion_sample_2==1 [pweight=dw_std], group(id) // ologit -- no. of observations唔同
	quietly estadd local fixedi "Yes", replace
}
esttab ologit_pos ologitcul_useful_ ologitcul_sol_ ologitcul_att_ ologitcul_like_ ///
	using "persuasion_lr+cul_feologit.rtf", b(2) se(2) replace ///
	scalars ("fixedi Individual FE" "N Observations" "r2_p pseudo R-squared") ///
	label wide note onecell nobaselevels  noomitted nonumbers  lines varwidth(30) ///
	order (LT  1.AfD#c.LT 1.mig_crisis 1.mig_crisis#1.AfD) ///
	coeflabels(1.AfD#c.LT "1st time AfD voters * subnational parliament"  ///
	1.mig_crisis "post-crisis" 1.mig_crisis#1.AfD "1st time AfD voters * post-crisis" ///
	1.mig_crisis#c.LT "post-crisis * subnational parliament" 1.mig_crisis#1.AfD#c.LT "1st time AfD voters * post-crisis * subnational parliament") ///
	mtitles("Model 1" "Model 2" "Model 3" "Model 4"	"Model 5") modelwidth(4) ///
	note ("Note: DV of Model 1: Left/right self-placement;" ///
	"DV of Model 2: Usefulness of cultural diversity;" ///
	"DV of Model 3: High cultural diversity can tackle new problems;" ///
	"DV of Model 4: Positive attitudes towards other cultures;" ///
	"DV of Model 5: Liking people from different cultural backgrounds.") ///
	title ("Table S3.2 Persuasion effect on left/right self-placement and cultural attitude (Ordinal logit model)") 


* ------------------------------------------------------------------------------
* Table S3.3 Correlation of different pairs of cultural attitude constructs and their differences across waves
use "data.dta", replace
keep id wave cul_useful_ cul_sol_ cul_att_ cul_like_ AfD
gen persuasion_sample_2 = 1 if cul_useful_!=. & cul_sol_!=. & cul_att_!=. & cul_like_!=. & ///
	cul_useful_!=.a & cul_sol_!=.a & cul_att_!=.a & cul_like_!=.a & AfD!=.

foreach i in 3 4 6{
gen wave_`i'=wave==`i'
}

foreach pair in "cul_useful_ cul_sol_" "cul_useful_ cul_att_" "cul_useful_ cul_like_" ///
	"cul_sol_ cul_att_" "cul_sol_ cul_like_" ///
	"cul_att_ cul_like_" {
	display "Wave 3 & 4" 
	covtest `pair' if AfD==1 & (wave==3 | wave==4) &  persuasion_sample_2 == 1, by(wave_4) // put the covtest package in your STATA ado file
}	

	
* ----------------------------------------------------------------------------
* Projection Effect
* ----------------------------------------------------------------------------
use "data.dta", replace
keep if wave==1 | wave==6
tsset id wave 

* generate regression sample
gen projection_sample_1 = 1 if pos_AfD_!=. & AfD!=.
* regression model testing if there is a change in perceived ideological position of AfD among 1st-time AfD voters and non-AfD voters
eststo projection: reg pos_AfD_ AfD##6.wave i.edu_ inc_ sex_ age_ polint_ region_ if projection_sample_1==1  [pweight=dw_std], vce(cluster id) 
lincom 1.AfD+6.wave+1.AfD#6.wave
lincom 6.wave+1.AfD#6.wave

* ------------------------------------------------------------------------------
* Figure 6. Change in perceived left/right position of AfD among first time AfD voters and non-AfD voters. 
margins wave, at(AfD=(0 1)) atmeans
marginsplot, xdim(wave) xtitle("Wave") plot2opts(lcolor("0 158 224"%70) mcolor("0 158 224"%70)) ci2opts(lcolor("0 158 224"%70)) ///
	ytitle("") ylabel(1(1)7) legend(position(6) col(2)) /// 
	title("Perceived left/right position of AfD", size(medsmall)) saving(AfD_lr,replace)
graph export "projection_sim.png",  as(png) replace

* ------------------------------------------------------------------------------
* Table S3.4 Projection effect: Effect of subnational parliament entry on the perceived left/right position of AfD
esttab  projection ///
	using "projection.rtf", b(2) se(2) replace ///
	scalars ("N Observations" "r2 R-squared") ///
	label wide note onecell nobaselevels  noomitted nonumbers  lines varwidth(30) ///
	order (1.AfD  6.wave 1.AfD#6.wave) ///
	drop(sex_ inc_ age_ 2.edu_ 3.edu_ 4.edu_ 5.edu_ 6.edu_ 7.edu_ 8.edu_ 9.edu_ polint_ region_) ///
	coeflabels(1.AfD#6.wave "1st time AfD voters * wave 6")  ///
	mtitles("Model 1") modelwidth(4) ///
	note ("Note: DV of Model 1: Perceived left/right position of AfD." ///
	"Entries are coefficients of OLS. Clustered standard errors are shown in parentheses. Typical socio-demographic variables are omitted due to space limitation.") ///
	title ("Table S3.4 Projection effect: Effect of subnational parliament entry on the perceived left/right position of AfD") 

* ----------------------------------------------------------------------------	
* Persuasion effect in East Germany 
* ----------------------------------------------------------------------------	
use "data.dta", replace
set scheme plotplain
* declare panel 
tsset id wave  
drop if wave==0 | wave ==6 // wave 0 is the pilot phase; wave 6 is the wave collected after the 2017 federal election 

* generate regression sample
gen persuasion_sample_1 = 1 if pos_!=. & AfD!=. & region!=.

* regression model testing if there is a change in left/right self-placement among 1st-time AfD voters and non-AfD voters
eststo ife_pos_east: xtreg pos_ c.East_LT i.mig_crisis c.East_LT#AfD c.East_LT#1.mig_crisis 1.mig_crisis#AfD i.mig_crisis#AfD#c.East_LT if persuasion_sample_1==1 & region==2 [pweight=dw_std], fe 

* the difference in self-placement between AfD voters and non-AfD voters at wave 1
predict fe_pos_east, u
tabulate AfD, summarize(fe_pos_east)

* ------------------------------------------------------------------------------
* Figure S3.1  panel a. Margin plot of left/right self-placement of 1st-time AfD voters and non-AfD voters across the 5 waves
margins if AfD==0, exp(predict(xb) - 0.07988038) at(mig_crisis=(0) East_LT=(0 0.5)) ///
	at(mig_crisis=(1) East_LT=(0.6666667 1)) noestimcheck saving(afd0 ,replace)
margins if AfD==1, exp(predict(xb) + 0.61757576) at(mig_crisis=(0) East_LT=(0 0.5)) ///
	at(mig_crisis=(1) East_LT=(0.6666667 1)) noestimcheck saving(afd1 ,replace)
combomarginsplot afd0 afd1, labels("non-AfD voters" "1st time AfD voters") ///
	xtitle("No. of subnational parliament entered") xlabel(0"0" 0.5"3" 0.6666667"4" 1"6") ///
	ytitle("Left/right self-placement") title("(a) Change in left/right self-placement")  legend(position(6) col(2))  ylabel(1(1)7) ///
	plot2opts(lcolor("0 158 224"%70) mcolor("0 158 224"%70)) ci2opts(lcolor("0 158 224"%70)) saving(persuasion_expanded_sim_east, replace) 

* ------------------------------------------------------------------------------
* t-test for right-wing shift in self-placement 
gen right1_east=.
replace right1_east=1 if persuasion_sample_1==1 & AfD==1 & wave==1 & pos_>4 & region==2 // 1st time AfD voters' whose ideological position is >4 on a 1-7 scale in wave 1
replace right1_east=0 if persuasion_sample_1==1 & AfD==1 & wave==1 & pos_<=4 & region==2 // 1st time AfD voters' whose ideological position is <=4 on a 1-7 scale in wave 1
tsfill
bysort id: carryforward right1_east, gen(right_1_east) 
drop right1_east
gen right_5_east=.
replace right_5_east=1 if persuasion_sample_1==1 & AfD==1 & wave==5 & pos_>4 & region==2 // 1st time AfD voters' whose ideological position is >4 on a 1-7 scale in wave 5
replace right_5_east=0 if persuasion_sample_1==1 & AfD==1 & wave==5 & pos_<=4 & region==2 // 1st time AfD voters' whose ideological position is <=4 on a 1-7 scale in wave 5

gen rwsample_east=1 if right_1_east!=. & right_5_east!=.
gen rw_sample_east = rwsample_east
gsort id -wave
by id : replace rw_sample_east = rw_sample_east[_n-1] if missing(rwsample_east)
drop rwsample_east
sum pos_ if rw_sample_east==1 & wave==1 [aweight=dw_std]
sum pos_ if rw_sample_east==1 & wave==5 [aweight=dw_std] 
reg right_5_east  right_1_east [pweight=dw_std]

* ------------------------------------------------------------------------------
* Figure S3.1  panel b. Histogram for right-wing shift in self-placement among 1st-time AfD voters
twoway  (hist pos_ if rw_sample_east==1 & wave==1, lcolor("0 0 0 %50") fcolor("0 0 0 %50") discrete) || /// 46 193 25
	(hist pos_ if rw_sample_east==1 & wave==5, lcolor("0 158 224 %60") fcolor("0 158 224 %60") discrete), ///  0 46 224
	xlabel(1(1)7) xtitle("Left/right self-placement") ///
	xline(4.056216, lcolor(black) lw(medthick)) xline(4.682162, lcolor("0 158 224") lpattern(solid) lw(medthick)) ///
	ytitle("Density") title("(b) left/right self-placement among first-time AfD voters") ///
	legend(order(1 "wave 1" 2 "wave 5" ) position(6) col(2)) saving(persuasion_hist_east, replace) 
graph combine persuasion_expanded_sim_east.gph persuasion_hist_east.gph
graph export "persuasion_east.png", as(png) replace

* ----------------------------------------------------------------------------	
* Persuasion effect in West Germany 
* ----------------------------------------------------------------------------	
* regression model testing if there is a change in left/right self-placement among 1st-time AfD voters and non-AfD voters
eststo ife_pos_west: xtreg pos_ c.West_LT i.mig_crisis c.West_LT#AfD c.West_LT#1.mig_crisis 1.mig_crisis#AfD i.mig_crisis#AfD#c.West_LT if persuasion_sample_1==1 & region==1 [pweight=dw_std], fe 
* the difference in self-placement between AfD voters and non-AfD voters at wave 1
predict fe_pos_west, u
tabulate AfD, summarize(fe_pos_west)

* ------------------------------------------------------------------------------
* Figure S3.2  panel a. Margin plot of left/right self-placement of 1st-time AfD voters and non-AfD voters across the 5 waves
margins if AfD==0, exp(predict(xb) - 0.05009879) at(mig_crisis=(0) West_LT=(0 0.2)) ///
	at(mig_crisis=(1) West_LT=(0.4 0.6 0.7)) noestimcheck saving(afd0 ,replace)
margins if AfD==1, exp(predict(xb) + 0.65437272) at(mig_crisis=(0) West_LT=(0 0.2)) ///
	at(mig_crisis=(1) West_LT=(0.4 0.6 0.7)) noestimcheck saving(afd1 ,replace)
combomarginsplot afd0 afd1, labels("non-AfD voters" "1st time AfD voters") ///
	xtitle("No. of subnational parliament entered") xlabel(0"0" 0.2"2" 0.4"4" 0.6"6" 0.7"7") ///
	ytitle("Left/right self-placement") title("(a) Change in left/right self-placement")  legend(position(6) col(2))  ylabel(1(1)7) ///
	plot2opts(lcolor("0 158 224"%70) mcolor("0 158 224"%70)) ci2opts(lcolor("0 158 224"%70)) saving(persuasion_expanded_sim_west, replace) 


* ------------------------------------------------------------------------------
* t-test for right-wing shift in self-placement 
gen right1_west=.
replace right1_west=1 if persuasion_sample_1==1 & AfD==1 & wave==1 & pos_>4 & region==1 // 1st time AfD voters' whose ideological position is >4 on a 1-7 scale in wave 1
replace right1_west=0 if persuasion_sample_1==1 & AfD==1 & wave==1 & pos_<=4 & region==1 // 1st time AfD voters' whose ideological position is <=4 on a 1-7 scale in wave 1
tsfill
bysort id: carryforward right1_west, gen(right_1_west) 
drop right1_west
gen right_5_west=.
replace right_5_west=1 if persuasion_sample_1==1 & AfD==1 & wave==5 & pos_>4 & region==1 // 1st time AfD voters' whose ideological position is >4 on a 1-7 scale in wave 5
replace right_5_west=0 if persuasion_sample_1==1 & AfD==1 & wave==5 & pos_<=4 & region==1 // 1st time AfD voters' whose ideological position is <=4 on a 1-7 scale in wave 5

gen rwsample_west=1 if right_1_west!=. & right_5_west!=.
gen rw_sample_west = rwsample_west
gsort id -wave
by id : replace rw_sample_west = rw_sample_west[_n-1] if missing(rwsample_west)
drop rwsample_west
sum pos_ if rw_sample_west==1 & wave==1 [aweight=dw_std]
sum pos_ if rw_sample_west==1 & wave==5 [aweight=dw_std] 
reg right_5_west  right_1_west [pweight=dw_std]

* ------------------------------------------------------------------------------
* Figure S3.2 panel b. Histogram for right-wing shift in self-placement among 1st-time AfD voters
twoway  (hist pos_ if rw_sample_west==1 & wave==1, lcolor("0 0 0 %50") fcolor("0 0 0 %50") discrete) || /// 46 193 25
	(hist pos_ if rw_sample_west==1 & wave==5, lcolor("0 158 224 %60") fcolor("0 158 224 %60") discrete), ///  0 46 224
	xlabel(1(1)7) xtitle("Left/right self-placement") ///
	xline(4.57732, lcolor(black) lw(medthick)) xline(4.85567, lcolor("0 158 224") lpattern(solid) lw(medthick)) ///
	ytitle("Density") title("(b) left/right self-placement among first-time AfD voters") ///
	legend(order(1 "wave 1" 2 "wave 5" ) position(6) col(2)) saving(persuasion_hist_west, replace) 
graph export "persuasion_hist_west.png", as(png) replace
graph combine persuasion_expanded_sim_west.gph persuasion_hist_west.gph
graph export "persuasion_west.png", as(png) replace



* ----------------------------------------------------------------------------
* Contrast effect 
* ---------------------------------------------------------------------------- 
use "data.dta", replace
* declare panel 
tsset id wave  

* Dummy for CDU defectors to AfD
gen AfD_2017=.
replace AfD_2017=1 if (BTW_2017==6 & BTW_2013==1) // CDU defectors to AfD
replace AfD_2017=0 if (BTW_2017==1 & BTW_2013==1) //loyal CDU voters
tab AfD_2017 BTW_2017, m
tab AfD_2017 BTW_2013, m

* keep variables of interest
keep id wave pos_ pos_CDU_pb pos_CDU_ Merkel_ CDU_enemy_ CDU_firm_  ///
	BTW_2017 AfD_2017 cul_useful_ cul_sol_ cul_att_ cul_like_ edu_ inc_ sex_ age_ polint_ region_ dw_std 
keep if (wave==0 | wave==1 | wave==2 | wave==3 | wave==6)

* label variables
label define lbAfD_2017 0 "loyal CDU voters" 1 "Defectors to AfD"
label values AfD_2017 lbAfD_2017

* generate regression sample
gen contrast_sample_1 = 1 if pos_CDU_!=. 
gen contrast_sample_2 = 1 if CDU_enemy_!=. & CDU_firm_!=. &  Merkel_!=.

* ------------------------------------------------------------------------------
* Table S3.5 Difference in covariates between CDU defectors to AfD and loyal CDU voters in wave 1 
reg pos_ AfD_2017 if wave==1 & contrast_sample_1==1 [pweight=dw_std] // defectors to AfD are more likely to position themselves as right-wing by 0.74 unit
reg polint_ AfD_2017 if wave==1 & contrast_sample_1==1 [pweight=dw_std] // n.s.
reg region_ AfD_2017 if wave==1 & contrast_sample_1==1 [pweight=dw_std] // defectors to AfD are more likely to be East German
reg age_ AfD_2017 if wave==1 & contrast_sample_1==1 [pweight=dw_std] // n.s.
reg sex_ AfD_2017 if wave==1 & contrast_sample_1==1 [pweight=dw_std] // defectors to AfD are more likely to be male
reg inc_ AfD_2017 if wave==1 & contrast_sample_1==1 [pweight=dw_std] // n.s.
forvalues i=1/9{ //  defectors to AfD are less likely to have university degree 
	tempvar edulv
	generate `edulv' = edu_==`i'
	replace `edulv' = . if edu_==.
	display "edu level == `i'"
	reg `edulv' AfD_2017 if wave==1 & contrast_sample_1==1 [pweight=dw_std]
}


* ------------------------------------------------------------------------------   
* regression model testing if there is a change in perceived ideological position of CDU among CDU defectors to AfD and loyal CDU voters 
eststo CDU_lr: reg pos_CDU_ AfD_2017##6.wave i.edu_ inc_ sex_ age_ polint_ region_ if AfD_2017!=. & contrast_sample_1==1 [pweight=dw_std], vce(cluster id) 

* ------------------------------------------------------------------------------
* Figure 7 panel a. Contrast effect in left/right placement
margins wave, at(AfD=(0 1)) atmeans
marginsplot, xdim(wave) xtitle("Wave") plot2opts(lcolor("0 158 224"%70) mcolor("0 158 224"%70)) ci2opts(lcolor("0 158 224"%70)) ///
	ytitle("") ylabel(1(1)7) legend(position(6) col(2)) /// 
	title("(a) Perceived left/right position of CDU", size(medsmall)) saving(CDU_lr,replace)

* ------------------------------------------------------------------------------   
* regression model testing if CDU defectors to AfD are more likely to perceive Party member of CDU/CSU as enemy than loyal CDU voters 
eststo enemy: reg CDU_enemy_ AfD_2017##3.wave i.edu_ inc_ sex_ age_ polint_ region_ if AfD_2017!=. & contrast_sample_2==1 [pweight=dw_std], vce(cluster id)	

* ------------------------------------------------------------------------------
* Figure 7 panel b. Contrast effect in party evaluation (Party member of CDU/CSU as enemy)
margins wave, at(AfD=(0 1)) atmeans 
marginsplot, xdim(wave) xtitle("Wave") plot2opts(lcolor("0 158 224"%70) mcolor("0 158 224"%70)) ci2opts(lcolor("0 158 224"%70)) ///
	ytitle("") ylabel(1(1)7) legend(position(6) col(2)) /// 
	title("(b) Party member of CDU/CSU as enemy", size(medsmall)) saving(enemy,replace)

* ------------------------------------------------------------------------------   
* regression model testing if CDU defectors to AfD are less likely to be firm supporter of CDU/CSU than loyal CDU voters 	
eststo firm: reg CDU_firm_ AfD_2017##3.wave i.edu_ inc_ sex_ age_ polint_ region_ if AfD_2017!=. & contrast_sample_2==1 [pweight=dw_std] , vce(cluster id)

* ------------------------------------------------------------------------------
* Figure 7 panel c. Contrast effect in party evaluation (Firm supporter of CDU/CSU)
margins wave, at(AfD=(0 1)) atmeans 
marginsplot, xdim(wave) xtitle("Wave") plot2opts(lcolor("0 158 224"%70) mcolor("0 158 224"%70)) ci2opts(lcolor("0 158 224"%70)) ///
	ytitle("") ylabel(1(1)7) legend(position(6) col(2)) /// / 
	title("(c) Firm supporter of CDU/CSU", size(medsmall)) saving(firm,replace)	

* ------------------------------------------------------------------------------   
* regression model testing if CDU defectors to AfD are more likely to see Merkel more negatively than loyal CDU voters 		
eststo Merkel: reg Merkel_ AfD_2017##3.wave i.edu_ inc_ sex_ age_ polint_ region_ if AfD_2017!=. & contrast_sample_2==1 [pweight=dw_std] , vce(cluster id) 

* ------------------------------------------------------------------------------
* Figure 7 panel d. Contrast effect in party evaluation (Candidate orientation: Merkel)
margins wave, at(AfD=(0 1)) atmeans
marginsplot, xdim(wave) xtitle("Wave") plot2opts(lcolor("0 158 224"%70) mcolor("0 158 224"%70)) ci2opts(lcolor("0 158 224"%70)) ///
	ytitle("") ylabel(1(1)7) legend(position(6) col(2)) /// 
	title("(d) Candidate orientation: Merkel", size(medsmall)) saving(Merkel,replace)

* ------------------------------------------------------------------------------
* Figure 7
grc1leg CDU_lr.gph enemy.gph firm.gph Merkel.gph
graph export "contrast effect.png", replace	

* ------------------------------------------------------------------------------
* Table 2. Contrast effect in left/right placement and in party evaluation
esttab  CDU_lr enemy firm Merkel ///
	using "contrast.rtf", b(2) se(2) replace ///
	scalars ("r2 R-squared") ///
	label wide note onecell nobaselevels  noomitted nonumbers  lines varwidth(30) ///
	order (1.AfD_2017 6.wave 1.AfD_2017#6.wave 3.wave 1.AfD_2017#3.wave) ///
	drop(sex_ inc_ age_ 3.edu_ 4.edu_ 5.edu_ 6.edu_ 7.edu_ 8.edu_ 9.edu_ polint_ region_) ///
	coeflabels(region_ "East Germany"  polint_ "political interest" ///
	1.AfD_2017#6.wave "Defectors to AfD * wave 6" 1.AfD_2017#3.wave "Defectors to AfD * wave 3") ///
	mtitles("Model 1" "Model 2" "Model 3" "Model 4") modelwidth(4) star(+ 0.10 * 0.05 ** 0.01 *** 0.001 ) ///
	note ("Note: DV of Model 1: Perceived left/right position of CDU;" ///
	"DV of Model 2: Seeing CDU/CSU party member as enemy;" ///
	"DV of Model 3: Firm supporter of the CDU/CSU; DV of Model 4: Candidate orientation towards Angela Merkel." ///
	"Entries are coefficients of OLS. Clustered standard errors are shown in parentheses." ///
	"Typical socio-demographic variables are omitted due to space limitation." ///
	"Sample restricted to CDU defectors to AfD and loyal CDU voters.") ///
	title ("Table 2. Contrast effect in left/right placement and in party evaluation") 
	
* ------------------------------------------------------------------------------
* Table 3 (The distribution of perceived position of CDU at wave 6 among loyal CDU voters and CDU defectors to AfD)
tab pos_CDU_ AfD_2017 if contrast_sample_1==1 & wave==6 [aweight=dw_std], col


* ------------------------------------------------------------------------------
* Placebo test for the contrast effect in left/right placement
keep if wave==0 | wave==1 // wave 0 is the pilot phase
replace AfD_2017=F.AfD_2017 if wave==0
replace pos_CDU_ = pos_CDU_pb if wave==0
eststo placebo: reg pos_CDU_ AfD_2017##1.wave i.edu_ inc_ sex_ age_ polint_ region_ if AfD_2017!=. [pweight=dw_std] , vce(cluster id)

* ------------------------------------------------------------------------------
* Table S3.6 Placebo test for the contrast effect in left/right placement
esttab  placebo ///
	using "contrast_placebo.rtf", b(2) se(2) replace ///
	scalars ("r2 R-squared") ///
	label wide note onecell nobaselevels  noomitted nonumbers  lines varwidth(30) ///
	order (1.AfD_2017 1.wave 1.AfD_2017#1.wave) ///
	drop(sex_ inc_ age_ 4.edu_ 5.edu_ 6.edu_ 7.edu_ 8.edu_ 9.edu_ polint_ region_) ///
	coeflabels(1.AfD_2017#1.wave "Defectors to AfD * wave 1") ///
	mtitles("Model 1" ) modelwidth(4) star(* 0.05 ** 0.01 *** 0.001 ) ///
	note ("Note: DV of Model 1: Perceived left/right position of CDU;" ///
	"Entries are coefficients of OLS. Clustered standard errors are shown in parentheses." ///
	"Typical socio-demographic variables are omitted." ///
	"Sample restricted to CDU defectors to AfD and loyal CDU voters.") ///
	title ("Table S3.6 Placebo test for the contrast effect in left/right placement") 